using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using PHSRAG.Utility;
using PHSRAG.Security.Enumerations;
using System.IO;
using System.Collections;

namespace PHSRAG.Insight21.Admin.Controls
{
	/// <summary>
	///	Assigns access rights to lab members.
	/// </summary>
	public partial class EditAdminLabAccessRights : System.Web.UI.UserControl
	{
		#region Constants
		/// <summary>Key used to check if the control is loaded for the first time or as a result of a client postback.</summary>
		private const string ControlPostBackKey = "ControlPostBackKey_02020B3B-ABD7-4cfb-8A12-2ED67E042DD7";
		#endregion

		#region Private Variables
		/// <summary>The reference to the containing page (.aspx). </summary>
		private EditAdminLabAccessWizard			hostPage;
		#endregion

		#region Private Properties
		/// <summary>
		/// Reinforces the notion of "IsPostBack" within this user control as UserControl.IsPostBack
		/// is the same as Page.IsPostBack which will not work in this case because we have one page and
		/// multiple controls.
		/// </summary>
		private bool IsUserControlPostBack 
		{
			get { return ViewState[ControlPostBackKey] != null; }
			set { ViewState.Add(ControlPostBackKey, value); }
		}
		#endregion

		#region Event Handlers
		/// <summary>
		/// Initializes the host page, set access rights checkboxes based on the
		/// lab access rights the members currently have and also sets the visibility
		/// of those rights based on module selection on modules page of the wizard. 
		/// </summary>
		/// <param name="sender">The object raising an event.</param>
		/// <param name="e">Argument associated with the event</param>
		protected void Page_Load(object sender, System.EventArgs e)
		{
			hostPage = (EditAdminLabAccessWizard)Page;
			if (!IsUserControlPostBack) 
			{			
				SetLabAccessRightsCheckBoxes();
				IsUserControlPostBack = true;

				agreementsRow.Visible	= hostPage.IsAgreementsModuleSelected;
				purchasingRow.Visible	= hostPage.IsPurchasingModuleSelected;
				humansRow.Visible		= hostPage.IsHumansModuleSelected;
			}
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Sets the access rights checkboxes based on lab's access rights currently set
		/// for the lab.
		/// </summary>
		private void SetLabAccessRightsCheckBoxes()
		{
			if( hostPage.CurrentLab.Access.Rows[0].RowState != System.Data.DataRowState.Deleted)
			{
				object labRightsBitMask = hostPage.CurrentLab.Access.Rows[0]["RightBitMask"];// same access rights for all lab members
				if (labRightsBitMask != DBNull.Value) 
				{				 
					viewFinInfo.Checked = 
						((int)labRightsBitMask & (int)AccessRightBit.ViewFinancialInformation) != 0;
					viewSalInfo.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ViewSalaryInformation) != 0;
					viewAccess.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ViewAccess) != 0;
					viewPendingAgr.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ViewPendingAgreements) != 0;
					viewProtInfo.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ViewProtocolInformation) != 0;
					editProtInfo.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.EditProtocolInformation) != 0;
					manageProtInfo.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ManageProtocolInformation) != 0;
					viewPeoplesoftInfo.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ViewPeoplesoftInformation) != 0;
					viewUserOrder.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ViewUserOrder) != 0;
					manageUserOrder.Checked =
						((int)labRightsBitMask & (int)AccessRightBit.ManageUserOrder) != 0;
				}
			}
		}
		#endregion

		#region Web Form Designer generated code
		/// <summary>
		/// Wires up page events.
		/// </summary>
		/// <param name="e">Information associated with the event.</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);

			ID = "editAdminLabAccessRights";
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}
		#endregion

	}
}
